/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocator xmlns:vm="clr-namespace:TrafficWatcher"
                           x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool's support.
  See http://www.galasoft.ch/mvvm
*/

using System.Diagnostics.CodeAnalysis;
using System.Windows;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using TrafficWatcher.ViewModels;

namespace TrafficWatcher.ViewModels
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        private static MainViewModel mainViewModel;
        private static SettingsViewModel settingsViewModel;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DesignDataService>();
            ////}
            ////else
            ////{
            ////    // Create run time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DataService>();
            ////}

            //SimpleIoc.Default.Register<SettingsViewModel>();

            CreateMainViewModel();
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (mainViewModel == null)
                {
                    CreateMainViewModel();
                }

                return mainViewModel;
            }
        }

        /// <summary>
        /// Gets the Settings property.
        /// </summary>
        public static SettingsViewModel SettingsStatic
        {
            get
            {
                if (settingsViewModel == null)
                {
                    CreateSettingsViewModel();
                }

                return settingsViewModel;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Gets the Settings property.
        /// </summary>
        [SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public SettingsViewModel Settings
        {
            get
            {
                return SettingsStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            mainViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to delete the Settings property.
        /// </summary>
        public static void ClearSettings()
        {
            settingsViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMainViewModel()
        {
            if (mainViewModel == null)
            {
                var app = (App) Application.Current;
                mainViewModel = new MainViewModel(app.Settings, app.EnvironmentContext, app.DirectionDurationService);
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the Settings property.
        /// </summary>
        public static void CreateSettingsViewModel()
        {
            if (settingsViewModel == null)
            {
                var app = (App)Application.Current;
                settingsViewModel = new SettingsViewModel(app.Settings, app.EnvironmentContext, app.DirectionsProviderRepository);
            }
        }

        
        public static void Cleanup()
        {
            ClearMain();
            ClearSettings();
        }
    }
}